package com.rrjjg.app.admin.bus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rrjjg.app.admin.bus.bo.PlatformBo;
import com.rrjjg.app.admin.bus.bo.TreeBo;
import com.rrjjg.app.admin.data.service.IPlatformService;
import com.rrjjg.app.admin.data.storage.entity.Platform;
import com.rrjjg.app.mybatis.bean.PageData;
import com.rrjjg.app.mybatis.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ----------------------------------------------------------------------------- <br>
 * 功能 :  <br>
 * ----------------------------------------------------------------------------- <br>
 * 作者 : Vayne <br>
 * 时间 : 2023/4/18 <br>
 */
@Component
public class BPlatformService {

    @Autowired
    IPlatformService iPlatformService;


    public PageData<Platform> queryPage(Platform platform, int page, int size){
        Page<Platform> platformPage = iPlatformService.queryPage(platform, page, size);
        return PageUtil.EtoVList(platformPage);
    }


    public List<TreeBo> tree() {
        List<TreeBo> list = new ArrayList<>();
        List<Platform> pls = iPlatformService.list();
        if(!CollectionUtils.isEmpty(pls)){
            Map<Long, List<Platform>> map = pls.stream().collect(Collectors.groupingBy(Platform::getPid));
            List<TreeBo> collect = map.get(0L).stream().map(pp -> {
                TreeBo treeDto = new TreeBo();
                treeDto.setPid(pp.getPid())
                        .setPlabel("")
                        .setLabel(pp.getName())
                        .setId(pp.getId());
                List<Platform> children = map.get(treeDto.getId());
                if (!CollectionUtils.isEmpty(children)) {
                    treeDto.setChildren(children.stream().map(pl -> new TreeBo()
                            .setId(pl.getId())
                            .setCode(pl.getCore())
                            .setLabel(pl.getName())
                            .setPid(treeDto.getId())
                            .setPlabel(treeDto.getLabel())).collect(Collectors.toList()));
                }
                return treeDto;
            }).collect(Collectors.toList());
            list.addAll(collect);
        }
        return list;
    }

    public PageData<PlatformBo> serviceList(Platform platform, int page, int size) {
        QueryWrapper queryWrapper = PageUtil.queryWrapper(platform, Platform.class);
        queryWrapper.gt("pid", 0L);
        Page ans = iPlatformService.page(new Page<>(page, size), queryWrapper);
        List<Platform> records = ans.getRecords();
        if(!CollectionUtils.isEmpty(records)) {
            List<Long> collect = records.stream().map(Platform::getPid).distinct().collect(Collectors.toList());
            Map<Long, String> map = iPlatformService.list(Wrappers.<Platform>lambdaQuery().in(Platform::getId, collect)).stream()
                    .collect(Collectors.toMap(Platform::getId, Platform::getName));
            List<PlatformBo> dtos = records.stream().map(pl -> {
                PlatformBo transformation = PageUtil.transformation(pl, PlatformBo::new);
                transformation.setPname(map.get(transformation.getPid()));
                return transformation;
            }).collect(Collectors.toList());
            ans.setRecords(dtos);
        }
        return  PageUtil.EtoVList(ans);
    }

    public IPlatformService getiPlatformService() {
        return iPlatformService;
    }
}
